<?php
namespace app\stores\model\promotion;

use app\common\model\CommonModel;

use think\facade\Db;

class TeamModel extends CommonModel{
    protected  $name = 'promotion_team';
    protected  $pk   = 'id';

    public static function list($where,$order,$page=1,$limit=10,$field='*'){
        $query = Db::name('promotion_team')
            ->where(['is_delete'=>0])
            ->when(!empty($where['is_effect']),function ($query) use ($where){
                $query->where(['is_effect'=>$where['is_effect']]);
            })
            ->when(isset($where['name']) && !empty($where['name']),function ($query) use ($where){
                $query->where('name','like','%'.$where['name'].'%');
            })
            ->field($field);
        $count = $query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
            }
        }
        return compact('count','list');
    }
    public static function alist($where,$order,$page=1,$limit=10){
        $query = Db::name('promotion_team')->alias('a')
            ->join('beautiful_member b','a.member_id=b.member_id')
            ->where(['a.is_delete'=>0])
            ->when(isset($where['is_effect']),function ($query) use ($where){
                $query->where(['a.is_effect'=>$where['is_effect']]);
            })
            ->when(isset($where['member_num']),function ($query) use ($where){
                $query->where(['b.member_num'=>$where['member_num']]);
            })
            ->when(isset($where['mobile']),function ($query) use ($where){
                $query->where(['a.mobile'=>$where['mobile']]);
            })
            ->when(isset($where['name']) && !empty($where['name']),function ($query) use ($where){
                $query->where('a.name','like','%'.$where['name'].'%');
            })
            ->field('a.*,b.face,b.member_num');
        $count = $query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                $s=[];
                $s['team_id']=$v['id'];
                $s['is_effect']=1;
                $s['is_delete']=0;
                $member_count=PromotionmemberModel::where($s)->count();
                $v['member_count']=$member_count;
            }
        }
        return compact('count','list');
    }
    public static function info($where){
        $info = Db::name('promotion_team')->alias('a')
            ->join('beautiful_member b','a.member_id=b.member_id')
            ->where(['a.is_delete'=>0])
            ->when(isset($where['is_effect']),function ($query) use ($where){
                $query->where(['a.is_effect'=>$where['is_effect']]);
            })
            ->when(isset($where['id']),function ($query) use ($where){
                $query->where(['a.id'=>$where['id']]);
            })
            ->when(isset($where['member_num']),function ($query) use ($where){
                $query->where(['b.member_num'=>$where['member_num']]);
            })
            ->when(isset($where['mobile']),function ($query) use ($where){
                $query->where(['a.mobile'=>$where['mobile']]);
            })
            ->when(isset($where['name']) && !empty($where['name']),function ($query) use ($where){
                $query->where('a.name','like','%'.$where['name'].'%');
            })
            ->field('a.*,b.face,b.member_num')->find();
        if(!empty($info)){
            $s=[];
            $s['team_id']=$info['id'];
            $s['is_effect']=1;
            $s['is_delete']=0;
            $count=PromotionmemberModel::where($s)->count();
            $info['member_count']=$count;
        }
        return $info;
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
    public function team_num(){
        $team_num=rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
        $info=self::where(['team_num'=>$team_num])->find();
        if(!empty($info)){
            return $this->team_num();
        }
        return $team_num;
    }
}
